/*
 * @Description: 路由中间件
 * @Author: wangqi
 * @Date: 2024-07-23 17:48:20
 * @LastEditTime: 2024-07-23 18:10:54
 * @LastEditAuthor: wangqi
 */

/* 

    针对 /admin  /setting 的请求, 要求 URL 携带 code=521 参数, 如未携带提示『暗号错误』  

    express 获取参数方法
    - params:   用于获取问号(query string)中的参数

    - query:    用于获取路径(path)中的参数

*/

const express = require('express')
const app = express()

function routerMiddleWare(req, res, next) {
    let { query } = req
    if (query.code == 521) {
        next()
    } else {
        res.send('暗号错误~')
    }
}



app.get('/admin', routerMiddleWare, (req, res) => {
    res.send('<h2>admin 路由</h2>')
})

app.get('/setting', routerMiddleWare, (req, res) => {
    res.send('<h2>setting 路由</h2>')
})

app.get('/home', (req, res) => {
    res.send('<h2>home 路由</h2>')
})

app.listen(3000, () => {
    console.log('Server is running on port 3000')
})